package com.xlf.common.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

@SuppressWarnings("serial")
public class Factory extends Model<Factory> {
	public static final Factory dao = new Factory();

	public Page<Factory> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *",
				"from factory order by f_id asc");
	}
	public Page<Factory> exeFactoryNameByOid(String o_id){
		
		//select name from factory where f_id = (select  f_id from orderitem where o_id = '1504242152205' limit 1);
		StringBuffer whee = new StringBuffer(" from factory where 1=1 ");
		List<Object> param = new ArrayList<Object>();
		whee.append(" and f_id = (select f_id from orderitem where 1=1 ");
		if (o_id != null && !"".equals(o_id.trim())) {
			whee.append(" and o_id = ?");
			param.add( o_id );
		}

		whee.append(" limit 1 ) order by f_id asc ");
		return paginate(1, 10, "select name ", whee.toString(),
				param.toArray());
	}
	public Page<Factory> exeProductByFid(int pageNumber, int pageSize, String name, int f_id) {
		
		StringBuffer whee = new StringBuffer(" from factory where 1=1 ");
		List<Object> param = new ArrayList<Object>();

		if (name != null && !"".equals(name.trim())) {
			whee.append(" and name like ?");
			param.add("%" + name + "%");
		}

		whee.append(" order by f_id asc ");
		return paginate(pageNumber, pageSize, "select *", whee.toString(),
				param.toArray());
	}

	public Factory exeFindByName(String username, String password) {
		StringBuffer whee = new StringBuffer(" from factory where 1=1 ");
		List<Object> param = new ArrayList<Object>();

		if (username != null && !username.equals("")) {
			whee.append(" and adminname = ?");
			param.add(username);
		}
		if (password != null && !password.equals("")) {
			whee.append(" and adminpassword = ?");
			param.add(password);
		}
		return paginate(1, 1, "select *", whee.toString(), param.toArray())
				.getList().get(0);
	}
}
